package com.elitel.frame.base.component;

import com.alibaba.fastjson.JSON;
import com.elitel.frame.base.service.*;
import com.elitel.frame.main.dao.ext.ExtCfgExtmasterMapper;
import com.elitel.frame.main.entity.*;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("cacheCollections")
public class CacheCollections {
    @Autowired
    private CfgExtendService cfgExtendService;
    @Autowired
    private CfgDbService cfgDbService;
    @Autowired
    private CfgTableService cfgTableService;
    @Autowired
    private CfgEditService cfgEditService;
    @Autowired
    private CfgExtsearchService cfgExtsearchService;
    @Autowired
    private ExtCfgExtmasterMapper extCfgExtmasterMapper;
    @Autowired
    private TreeConfigFileService treeConfigFileService;
    @Autowired
    private BasicServerService basicServerService;
    @Autowired
    private CodeService codeService;


    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_EXTEND'")//使用springcache缓存
    public List<CfgExtend> getCfgExtends() {
        List<CfgExtend> list = cfgExtendService.selectAllExtend();
        return list;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_DB'")
    public List<CfgDb> getCfgDBs() {
        List<CfgDb> list = cfgDbService.getAllListCfgDb();
        return list;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_TABLE_'+#key")
    public List<CfgTable> getCfgTables(String key) {
        List<CfgTable> list = cfgTableService.getListbyConn(key);
        return list;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_EDIT'")
    public List<CfgEdit> cfgEdits() {
        List<CfgEdit> cfgEditList = cfgEditService.selectAllCfgEdit();
        return cfgEditList;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_EXTSEARCH'")
    public List<CfgExtsearch> getCfgExtSearchs() {
        List<CfgExtsearch> cfgExtsearchList = cfgExtsearchService.selectAllCfgExtSearch();
        return cfgExtsearchList;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_EXTMASTER'")
    public List<CfgExtmaster> getCfgMsaters() {
        List<CfgExtmaster> list = extCfgExtmasterMapper.selectAllExtMaster();
        return list;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_TREECONFIG_'+#dbKey")
    public List<BaseTreeconfig> getBaseTrees(String dbKey) {
        PageResponseVo<BaseTreeconfig> pageResponseVos = treeConfigFileService.selectTreeConfigAll(dbKey);
        List<BaseTreeconfig> list = pageResponseVos.getRows();
        return list;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_DB'")
    public List<CfgDb> getCfgDbs() {
        List<CfgDb> cfgDbs = treeConfigFileService.selectCfgDbList();
        return cfgDbs;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'CFG_TABLE_ALL'")
    public List<CfgTable> getAllCfgTables() {
        List<CfgTable> list = treeConfigFileService.selectCfgTableAll();
        return list;
    }

    @Cacheable(cacheNames = CacheConfig.DEFAULT, key = "'TREE_TREECONFIG_ALL'")
    public List<CfgExttree> getAllTreesExt() {
        List<CfgExttree> list = treeConfigFileService.getTreeListaLL();
        return list;
    }

    @Cacheable(value = CacheConfig.DEFAULT, key = "'BASICSERVER'")
    public String getBasicServerStr() {
        PageResponseVo<BaseServices> pageResponseVos = basicServerService.selectBasicServerAll();
        String basicServerString = JSON.toJSONString(pageResponseVos);
        return basicServerString;
    }

    @Cacheable(cacheNames = "DEFAULT", key = "'CODE_'+#sysId")
    public String getCodeSystem(String sysId) {
        PageResponseVo<CodeSystem> codeAll = codeService.getAllCode(sysId);
        String codeString = JSONObject.fromObject(codeAll).toString();
        return codeString;
    }
}
